package com.meilai.project.service.middleware.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.entity.middleware.MessageSms;
import com.meilai.project.exception.CommonException;
import com.meilai.project.mapper.middleware.MessageSmsMapper;
import com.meilai.project.service.middleware.MessageSmsService;
import com.meilai.project.util.RegexUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 张驰
 * @date 2020/12/3 10:34
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements MessageSmsService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void send(String mobile, String text) {
        if (StringUtils.isNotEmpty(mobile)) {
            if (!RegexUtil.isMatch(RegexUtil.REGEX_MOBILE_SIMPLE, mobile)) {
                throw new CommonException("不合法的手机号码");
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("mobile", mobile);
            jsonObject.put("text", text);

            amqpTemplate.convertAndSend("meilai.sms.fanout.exchange", "", jsonObject.toString());
        }
    }

    @Override
    public void sendByTPL(String mobile, String tpl_id, String tpl_value) {
        if (StringUtils.isNotEmpty(mobile)) {
            if (!RegexUtil.isMatch(RegexUtil.REGEX_MOBILE_SIMPLE, mobile)) {
                throw new CommonException("不合法的手机号码");
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("mobile", mobile);
            jsonObject.put("tpl_id", tpl_id);
            jsonObject.put("tpl_value", tpl_value);

            amqpTemplate.convertAndSend("meilai.sms.fanout.exchange", "", jsonObject.toString());
        }
    }

    @Override
    public void record(String tenantName, MessageSms sms) {
        sms.setCreated_at(LocalDateTime.now());
        sms.setUpdated_at(sms.getCreated_at());
        save(sms);
    }

    @Override
    public void record(MessageSms sms) {
        sms.setCreated_at(LocalDateTime.now());
        sms.setUpdated_at(sms.getCreated_at());
        save(sms);
    }

    @Override
    public void sendVisit(String mobile, String tpl_id, String tpl_value, String tenantName) {
//        Optional<HttpSession> session = Optional.ofNullable(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession(false));
        if (StringUtils.isNotEmpty(mobile)) {
            if (!RegexUtil.isMatch(RegexUtil.REGEX_MOBILE_SIMPLE, mobile)) {
                throw new CommonException("不合法的手机号码");
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("mobile", mobile);
            jsonObject.put("tpl_id", tpl_id);
            jsonObject.put("tpl_value", tpl_value);
//            String tenantName = session.isPresent() ? (session.get().getAttribute("tenantName") != null ? session.get().getAttribute("tenantName").toString() : "master") : "master";
            jsonObject.put("tenantName", tenantName);
            amqpTemplate.convertAndSend("meilai.sms.fanout.exchange", "", jsonObject.toString());
        }
    }
}
